const userModel = require('../models/users')
const Result = require('../beans/result')

// 处理注册请求
function doRegister(users, callback) {
	userModel.deleteUser(users, (err, res) => {
		// console.log(res)
		if (!err) {
			callback({ code: 200, msg: '注销成功' })
		} else {
			callback({ code: 400, msg: err })
		}
	})
}
// 查询用户是否存在
function dofinduser(callback) {
	userModel.getUser((err, res) => {
		if (res) {
			callback(res)
		} else {
			callback(err)
		}
	})
}
function doLogin(user, callback) {
	console.log('dologin=', user)
	userModel.getUser(user, (err, res) => {
		if (res) {
			callback(new Result(200, err, null, '登录成功'))
		} else {
			callback(
				new Result(
					err ? 400 : 600,
					err ? '服务器错误' : '账号或密码错误',
					'',
					'登录失败'
				)
			)
		}
	})
}
module.exports = {
	doRegister,
	dofinduser,
	doLogin,
}
